package ui.swing.longwork;

import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;
import javax.swing.text.BadLocationException;

public class ProgressBarDemo extends JFrame{

	private static final long serialVersionUID = -3690113398121890857L;

	public final static int ONE_SECOND = 1000;
	
	private JProgressBar progressBar;
	private Timer timer;
	private JButton stButton;
	private LongTask task;
	private JTextArea taskOutput;
	private String newline = "\n";
	
	public ProgressBarDemo() {
		super("ProgressBarDemo");
		task = new LongTask();
		
		// Create the demo's UI
		stButton = new JButton("Start");
		stButton.setActionCommand("start");
		stButton.addActionListener(new ButtonListener());
		
		
		progressBar = new JProgressBar(0, task.getLengthOfTask());
		progressBar.setValue(0);
		progressBar.setStringPainted(true);
		
		taskOutput = new JTextArea(5, 20);
		taskOutput.setMargin(new Insets(5, 5, 5, 5));
		taskOutput.setEditable(false);
		
		JPanel panel = new JPanel();
		panel.add(stButton);
		panel.add(progressBar);
		
		JPanel contentPane = new JPanel();
		contentPane.setLayout(new BorderLayout());
		contentPane.add(panel, BorderLayout.NORTH);
		contentPane.add(new JScrollPane(taskOutput), BorderLayout.CENTER);
		contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
		setContentPane(contentPane);
				
		pack();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	/**
	 * The actionPerformed method in this class
	 * is called when the user presses the start button
	 */
	class ButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if(stButton.getActionCommand().equals("start"))	{
				stButton.setText("Stop");
				stButton.setActionCommand("stop");
				try {
					taskOutput.getDocument().remove(0, taskOutput.getText().length());
				} catch (BadLocationException e1) {
					e1.printStackTrace();
				}
				task.go();
				// Create a timer
				timer = new Timer(ONE_SECOND, new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						updateUIAccrodingToProgress();
					}
				});
				timer.start();
				return;
			} else if(stButton.getActionCommand().equals("stop")) {
				task.interupt();
				stButton.setText("Start");
				stButton.setActionCommand("start");
				return;
			}
		}
	}
	
	private void updateUIAccrodingToProgress() {
		switch(task.getStatus()) {
		case LongTask.NEW:
		case LongTask.ONGOING:
			progressBar.setValue(task.getCurrent());
			taskOutput.append(task.getMessage() + newline);
			taskOutput.setCaretPosition(taskOutput.getDocument().getLength());
			break;
		case LongTask.FINISHED:
			taskOutput.append("任务完成！");
			resetUI();
			break;
		case LongTask.INTERRUPTED:
			Toolkit.getDefaultToolkit().beep();
			taskOutput.append("用户中断！");
			resetUI();
			break;
		}
	}
	
	private void resetUI() {
		timer.stop();
		stButton.setText("Start");
		stButton.setActionCommand("start");
		progressBar.setValue(progressBar.getMinimum());
	}
	
	public static void main(String[] args) {
		new ProgressBarDemo();
	}
}
